<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<jsp:include page="../inc.jsp"></jsp:include>
</head>
<body>
	<form id="uploadimg-form"
		action="${pageContext.request.contextPath}/ContractController/batchContract"
		enctype="multipart/form-data" type="post">

		<input type="file" title="选择文件" name="contractExcleFile"
			id="contractExcleFile" /><input id="fileBtn" type="button"
			value="上传" onclick="uploadFile();" />


	</form>

	<div id="uploadresult"></div>
	</div>

	<script type="text/javascript">
		$(function() {
			//进入页面后关闭进度条提示框
			parent.$.messager.progress('close');

		});
		var tabs;
		function uploadFile() {
			
			
			var fileName = $('#contractExcleFile').val();
			if ('' == fileName) {
				alert("请选择上传文件");
				return;
			}
			var bingIndex = fileName.lastIndexOf('.');
			if (-1 == bingIndex) {
				alert("上传文件格式错误");
				return;
			}
			var suffix = fileName.substring(bingIndex + 1, fileName.length);
			if ('xls' == suffix) {
				$.ajaxFileUpload({
					//处理文件上传操作的服务器端地址
					url : '${pageContext.request.contextPath}/ContractController/batchContract',
					secureuri : false, //是否启用安全提交,默认为false
					fileElementId : 'contractExcleFile', //文件选择框的id属性
					dataType : 'json', //服务器返回的格式,可以是json或xml等
					success : function(data, status) { //服务器响应成功时的处理函数
						
						//首先判断是否已经创建了对应的tabs，如果创建了则删除掉
						if ($('#uploadresult').attr('class') != undefined) {
							tabs.tabs('close',0);
							tabs.tabs('close',0);
						}
						//增加一个TAB,分别包含成功和失败的
						tabs = $("#uploadresult").tabs({
							border : false
						});
						tabs.tabs('add', {
							title : '<span style=\"color:#33FF00;\">成功' + data.obj.successResult.length,
							closable : false
						});
						tabs.tabs('add', {
							title : '<span style=\"color:#F00;\">失败' + data.obj.faildResult.length + "</span>",
							closable : false
						});
						var successTabPanel = tabs.tabs('getTab', 0);
						var faildTabPanel = tabs.tabs('getTab', 1);
						var successdynamicTable = $('<table id="success"></table>');
						var faildynamicTable = $('<table id="faild"></table>');
						successTabPanel.html(successdynamicTable);
						faildTabPanel.html(faildynamicTable);

						var successDatagird = successdynamicTable.datagrid({
							title : '上传结果',
							fitColumns : true,
							rownumbers : true,
							frozenColumns : [ [ {
								field : 'contractName',
								title : '合同名称',
								width : 200
							} ] ],
							columns : [ [ {
								field : 'po',
								title : '合同编号',
								width : 150
							}, {
								field : 'contractMoney',
								title : '合同金额',
								align : 'center',
								width : 80,
								formatter : function(value, row, index) {

									return (accounting.formatNumber(value));
								}

							}, {
								field : 'creatDate',
								title : '合同开始时间',
								align : 'center',
								width : 80,
								sortable : true,
								formatter : function(value, row, index) {
									if (value != null) {
										value = new Date(value).format("yyyy-MM-dd");
									}
									return value;
								}
							}, {
								field : 'cexpirationDate',
								title : '约定验收时间',
								align : 'center',
								width : 80,
								sortable : true,
								formatter : function(value, row, index) {
									if (value != null) {
										value = new Date(value).format("yyyy-MM-dd");
									}
									return value;
								}
							} ] ]
						});
						successDatagird.datagrid('loadData', data.obj.successResult);

						var faildDatagird = faildynamicTable.datagrid({
							title : '上传结果',
							fitColumns : true,
							rownumbers : true,
							frozenColumns : [ [ {
								field : 'contractName',
								title : '合同名称',
								width : 200
							} ] ],
							columns : [ [ {
								field : 'po',
								title : '合同编号',
								width : 150
							}, {
								field : 'contractMoney',
								title : '合同金额',
								align : 'center',
								width : 80,
								formatter : function(value, row, index) {

									return (accounting.formatNumber(value));
								}

							}, {
								field : 'extendscloum',
								title : '失败原因',
								align : 'center',
								width : 80,
							} ] ]
						});
						faildDatagird.datagrid('loadData', data.obj.faildResult);

					},
					error : function(data, status, e) { //服务器响应失败时的处理函数
						$('#uploadresult').html("<span style=\"color:#F00;\">" + result.msg + "</span>");
					}
				});
			} else {
				alert("上传文件必须是已“xls”为后缀的Excel文件");
			}
		}
	</script>
</body>
</html>